package com.palfund.tablayoutfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private int curIndex = 0;
    private ArrayList<BlankFragment> mList;
    private Context mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] titles = getResources().getStringArray(R.array.titles);
        mList = new ArrayList<>();
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        for (int i = 0; i < titles.length; i++) {
            BlankFragment fragment = BlankFragment.newInstance(i);
            mList.add(fragment);
            TabLayout.Tab tab = tabLayout.newTab().setText(titles[i]).setIcon(R.drawable.selector);
            tabLayout.addTab(tab);
        }
        tabLayout.getTabAt(0).select();
        getSupportFragmentManager().beginTransaction().add(R.id.layout_container, mList.get(0))
                .commit();
        // 添加监听器
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switchFragment(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        ///////////////////////////////////////////////////////////////////////////
        // TabLayout
        ///////////////////////////////////////////////////////////////////////////
        //        // 创建tab
        //        TabLayout.Tab tab = tabLayout.newTab();
        //        // 添加tab
        //        tabLayout.addTab(tab);
        //        // 删除tab
        //        tabLayout.removeTab(tab);
        //        // 删除所有tab
        //        tabLayout.removeAllTabs();
        //        // 删除指定位置tab
        //        tabLayout.removeTabAt(0);
        //        // 通过position获得tab
        //        TabLayout.Tab tabAt = tabLayout.getTabAt(0);
        //        // 标签总数
        //        int tabCount = tabLayout.getTabCount();
        //        // 获得选择的tab的position
        //        int selectedTabPosition = tabLayout.getSelectedTabPosition();
        ///////////////////////////////////////////////////////////////////////////
        // Tab
        ///////////////////////////////////////////////////////////////////////////
        //        // 设置文字
        //        tab.setText("go");
        //        // 设置icon
        //        tab.setIcon(R.mipmap.ic_launcher_round);
        //        // 设置标签
        //        tab.setTag("u");
        //        // 获取标签
        //        Object tag = tab.getTag();
        //        // 获得position
        //        int position = tab.getPosition();
        //        // 判断是否被选择
        //        boolean selected = tab.isSelected();
        //        // 设置为被选择状态
        //        tab.select();
        //        // 自定义tab
        //        View view = LayoutInflater.from(this).inflate(R.layout.tab, null);
        //        tab.setCustomView(view);
        //        tabLayout.addTab(tab, 0);
    }

    private void switchFragment(int targetIndex) {
        //获取transaction对象
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //获取目标Fragment
        Fragment targetFragment = mList.get(targetIndex);
        //获取当前Fragment
        Fragment curFragment = mList.get(curIndex);
        //隐藏当前Fragment
        transaction.hide(curFragment);
        //判断目标Fragment是否已经添加过,添加过就显示,否则就添加
        if (targetFragment.isAdded()) {
            transaction.show(targetFragment);
        } else {
            transaction.add(R.id.layout_container, targetFragment);
        }
        //transaction  commit后对Fragment的操作才生效
        transaction.commit();
        curIndex = targetIndex;
    }
}
